﻿using Aop.Api.EmailSend;
using System;
using System.BLL;
using System.Collections.Generic;
using System.Dto;
using System.IBLL;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace System.MVC.Controllers
{
    public class ChargingController : Controller
    {
        
        public ActionResult ExpenseInfoList()
        {
            IChargingManager manager = new ChargingManager();
            var data = manager.GetExpenseTypeLists();
            ViewBag.data = data;
            return View();
        }

        public JsonResult GetExpenseInfoToCostDeliveryList(int limit, int page, string expenseType, string expenseState, string paymentChannel)
        {
            IChargingManager manager = new ChargingManager();
            int datalet;
            var data = manager.GetCostDeliveryDtoLists(limit, page, out datalet, expenseType, expenseState, paymentChannel);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AddExpenseInfo()
        {
            IChargingManager manager = new ChargingManager();
            var data = manager.GetExpenseTypeLists();
            ViewBag.data = data;
            return View();
        }

        [HttpPost]
        public JsonResult AddExpenseInfo(ExpenseInfoToCostDeliveryDto dto)
        {
            IChargingManager manager = new ChargingManager();
            manager.AddOneExpenseInfo(dto);
            return Json(new { code = 0, msg = "新增成功" });
        }

        [HttpPost]
        public JsonResult SetEmailToDebtor(int id)
        {
            IChargingManager Cmanager = new ChargingManager();
            IHouseholdManager Hmanager = new HouseholdManager();
            var ExpenseInfo = Cmanager.GetOneExpenseInfo(id);
            if (ExpenseInfo.expenseType == "停车费")
                return Json(new { code = 1, msg = "停车费收费不发邮件" });
            var houseLs = Hmanager.GetOneHouseholdById(int.Parse(ExpenseInfo.expenseBelong));
            SendEmailHelper helper = new SendEmailHelper();
            EmailContextDto contextDto = new EmailContextDto();
            contextDto.MailTitle = $"{ExpenseInfo.expenseType}催收";
            contextDto.mailContent = $"敬爱的{houseLs.UserName}业主，你于{ExpenseInfo.createTime}欠下的{ExpenseInfo.expenseType}{ExpenseInfo.expenseNumber}费用仍未结清，请尽快付款，祝您生活愉快！";
            contextDto.mailTo = houseLs.Email;

            var result = helper.SendEmail(contextDto);

            return Json(new { code = 0, msg = result });
        }

    }
}